/**
 * @author helio
 */
var ClientManageView = Ext.extend(BasicPanel,{
	inputWin : null,
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'clients',
					remoteSort: true,
			        fields: ['id','code','name', 'tel', 'address', 'email']
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
				
				plugins: new Ext.ux.ProgressBarPager()
		    });
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				columns: [csm,{
					header: "客户编号",
					dataIndex: 'code',
					width: 60,
					sortable: true
				}, {
					header: "客户名称",
					dataIndex: 'name',
					sortable: true
				}, {
					header: "电话",
					dataIndex: 'tel',			
					width: 80,
					sortable: true
				}, {
					header: "地址",
					dataIndex: 'address',
					width: 80,
					sortable: true
				}, {
					header: "Email",
					dataIndex: 'email',
					sortable: true
				}],
				
				sm: csm,
				
				tbar:[{
		            text:'添加客户',
		            tooltip:'添加新客户资料',
		            iconCls:'add',
					handler: this.newClient.createDelegate(this)
		        }, '-', {
		            text:'修改客户',
		            tooltip:'修改客户资料',
		            iconCls:'option',
					handler: this.modifyClient.createDelegate(this)
		        },'-',{
		            text:'删除客户',
		            tooltip:'删除选中的客户资料',
		            iconCls:'remove',
					handler: this.deleteSelectedClients.createDelegate(this)
		        }, '->', '-', new Ext.form.ComboBox({
					id: 'clientquerytype',
					tpl: comboxTpl,
				    triggerAction: 'all',
				    lazyRender:true,
				    forceSelection:true,
                 	selectOnFocus: true,
				    mode: 'local',
				    width: 100,
				    store: new Ext.data.ArrayStore({
				        id: 0,
				        fields: [
				            'code',
				            'name'
				        ],
				        data: [[1, '按编号查询'], [2, '按名称查询'], [3, '按电话查询'], [4, '按地址查询']]
				    }),
				    valueField: 'code',
				    displayField: 'name',
				    value:1
				}), '-', {
					id: 'clientqueryvalue',
                	xtype:'trigger',
                    triggerClass: 'x-form-search-trigger',
                    width: 100,
                    onTriggerClick: this.queryClients.createDelegate(this),
                    listeners: {
        				'show': this.queryClients.createDelegate(this)
        			}
                }],
				
				bbar: pagingBar,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
		
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'name',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this),
					
					rowdblclick : this.modifyClient.createDelegate(this)
				}
				
			});
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['clients'] = resultJson.clients;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.getClientsDetailsJson(this.initlizeDataHandler.createDelegate(this));
		}
		return this.detailsGrid;
	},
	
	queryClients : function() {
		var querytype = Ext.getCmp('clientquerytype').getValue();
		var queryvalue = Ext.getCmp('clientqueryvalue').getValue();
		var condition = {};
		if(querytype == 1) {
			condition.code = queryvalue;
		} else if(querytype == 2) {
			condition.name = queryvalue;
		} else if(querytype == 3) {
			condition.tel = queryvalue;
		} else if(querytype == 4) {
			condition.address = queryvalue;
		} 
		
		if(queryvalue){
			SALE.Action.getClientsDetailsJson(this.initlizeDataHandler.createDelegate(this), condition);
		}
	},
	
	showEditeWin : function() {
		
		SALE.window.showClientEditWindow(this.refreshGridRecord.createDelegate(this),this.formDataJson, this.task);
	},
	
	newClient : function() {
		this.task = "new";
		this.formDataJson = {};
		this.showEditeWin();
	},
	
	modifyClient : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			SALE.Utils.showMessageBox('请先选择一条数据！');
		} else if(recordList.length > 1) {
			SALE.Utils.showMessageBox('每次只能编辑一条数据，请确认只选中了一条数据！');
		} else {
			var id= recordList[0].get("id");
			
			SALE.Action.searchClientJson(this.showModifyDataHandler.createDelegate(this), id);				
		}
	},
	
	deleteSelectedClients : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			SALE.Utils.showMessageBox('请选择你要删除的数据！');
		} else {
			Ext.MessageBox.confirm('删除确认', '你真的要删除选中的商品信息吗?', function(btn) {
				if (btn == "yes") {
					SALE.Action.deleteClientsInfo(this.deleteSuccessHandler.createDelegate(this), this.selectedRecordList);
				}
			}.createDelegate(this));
		}
	}
});

function showClientManagePanel(){
	var  manageView = new ClientManageView();
	
	SALE.App.showOperateTabPanel(manageView.getCorePanel(), "客户档案管理");
}